package com.hogocloud.corelibrary.base

import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProviders
import com.hogocloud.corelibrary.R
import com.hogocloud.corelibrary.common.dialog.LoadingDialog
import kotlinx.android.synthetic.main.fragment_base.*
import kotlinx.android.synthetic.main.fragment_base.view.*
import kotlinx.android.synthetic.main.layout_common_toolbar.*

/**
 * base fragment
 *
 * @author gjc
 * @version 1.0.0
 * @since 2019-08-04
 */
abstract class BaseFragment : Fragment(), IBaseViewModelEventObserver {

    companion object {
        private var mLoadingView: LoadingDialog? = null
    }

    protected lateinit var mBtnToolbarRight: TextView
    protected lateinit var mTvToolbarRight: TextView
    protected lateinit var mIvToolbarRight: ImageView

    protected var mContext: Context? = null
    protected var mActivity: Activity? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = context
        mActivity = context as Activity
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val rootView = inflater.inflate(R.layout.fragment_base, container, false)
        val contentView = inflater.inflate(initLayout(), container, false)
        rootView.fl_container.addView(contentView)
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //使内容占据整个屏幕，状态栏字体颜色为黑色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            activity?.window?.decorView?.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
        initView(view, savedInstanceState)
        initViewModelEvent()
    }

    /**
     * 初始化布局
     */
    abstract fun initLayout(): Int

    /**
     * 初始化布局组建
     */
    abstract fun initView(view: View, savedInstanceState: Bundle?)

    override fun getLifecycleOwner(): LifecycleOwner = this

    /**
     * 初始化toolbar
     * 只要调用该方法， toolbar就会显示，
     * 右边文字或者按钮需要单独设置显示状态
     */
    fun initTitle(@StringRes title: Int) {
        tv_toolbar_title.text = getString(title)
        mTvToolbarRight = tv_toolbar_right
        mBtnToolbarRight = btn_toolbar_right
        mIvToolbarRight = iv_toolbar_right
        layout_title.visibility = View.VISIBLE
        iv_act_back.setOnClickListener { activity?.finish() }
    }

    fun <T : BaseViewModel> getViewModel(clazz: Class<T>) =
        ViewModelProviders.of(this).get(clazz)

    /**
     * 显示加载框
     */
    override fun showLoading(msg: String) {
        mLoadingView = LoadingDialog.Builder(mContext ?: return)
            .setCancelOutside(false)
            .setCancelable(true)
            .create()
        mLoadingView?.show()
    }

    /**
     * 消失加载框
     */
    override fun hideLoading() {
        mLoadingView?.dismiss()
    }

    /**
     * 加载错误布局
     */
    override fun stateError() {

    }

    /**
     * 无网络布局
     */
    override fun stateNoNetWork() {

    }

    /**
     * 空页面
     */
    override fun stateEmpty() {

    }

    /**
     * 页面加载中布局（和showLoading使用其一）
     */
    override fun stateLoading() {

    }

    /**
     * 显示内容页面
     */
    override fun stateMain() {

    }

    override fun onViewStateRestored(savedInstanceState: Bundle?) {
        super.onViewStateRestored(savedInstanceState)
    }

}